package theOrbalEra.client;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiMainMenu;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.StringTranslate;

import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;
import org.lwjgl.util.glu.Sphere;

import theOrbalEra.client.OrbalArts.entity.OrbalArtEntityForte;
import theOrbalEra.client.OrbalArts.entity.OrbalArtEntityNapalmBreath;
import theOrbalEra.client.OrbalArts.entity.OrbalArtEntityFlameArrow;
import theOrbalEra.client.OrbalArts.entity.OrbalArtEntityFireBolt;
import theOrbalEra.client.OrbalArts.render.OrbalRenderForte;
import theOrbalEra.client.OrbalArts.render.OrbalRenderNapalmBreath;
import theOrbalEra.client.OrbalArts.render.OrbalRenderFlameArrow;
import theOrbalEra.client.OrbalArts.render.OrbalRenderFireBolt;
import theOrbalEra.client.gui.GuiTheOrbalEra;
import theOrbalEra.client.handler.KeyHandler;
import theOrbalEra.client.render.tileentity.TileEntityRenderStreetLight;
import theOrbalEra.common.CommonProxy;
import theOrbalEra.common.TickHandler;
import theOrbalEra.common.block.TileEntityStreetLight;
import theOrbalEra.common.network.PacketPlayerDataSave;
import theOrbalEra.common.player.PlayerHandler;
import theOrbalEra.common.player.PlayerStatistics;
import theOrbalEra.common.util.OrbalUtils;
import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.client.registry.ClientRegistry;
import cpw.mods.fml.client.registry.KeyBindingRegistry;
import cpw.mods.fml.client.registry.RenderingRegistry;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.registry.TickRegistry;
import cpw.mods.fml.relauncher.Side;

public class ClientProxy extends CommonProxy {
	public boolean loadedMod;
	private boolean changedGui;
	private boolean ranGame;
	public static int sphereID;
	public static int cylinderID;
	@Override
	public void registerRenderInformation() {
		registryOrbalArt();
		ClientRegistry.bindTileEntitySpecialRenderer(TileEntityStreetLight.class,new TileEntityRenderStreetLight());
		spawnSphere();

	}

	private void spawnSphere() {
		Sphere sphere = new Sphere();

	    sphere.setDrawStyle(GLU.GLU_FILL);

	    sphere.setNormals(GLU.GLU_LINE);

	    sphere.setOrientation(GLU.GLU_OUTSIDE);

	    sphere.setTextureFlag(true);

	    sphereID = GL11.glGenLists(1);

	    GL11.glNewList(sphereID, GL11.GL_COMPILE);

	    GL11.glTranslatef(0.5F, 0.5F, 0.5F);

	    sphere.draw(0.5F, 12, 24);

	    GL11.glEndList();
		
	}

	private int inTick=0;
	private String sessionPlayer; // Can only be used once the player has been in a game.
	@Override
	public void onLoad() {
			KeyBindingRegistry.registerKeyBinding(new KeyHandler());
			TickRegistry.registerTickHandler(new TickHandler(), Side.CLIENT);
			TickRegistry.registerTickHandler(new TickHandler(), Side.SERVER);
			
		
			
			
		
	}
	private void registryOrbalArt() {
		RenderingRegistry.registerEntityRenderingHandler(OrbalArtEntityFireBolt.class, new OrbalRenderFireBolt());
		RenderingRegistry.registerEntityRenderingHandler(OrbalArtEntityFlameArrow.class,new OrbalRenderFlameArrow());
		RenderingRegistry.registerEntityRenderingHandler(OrbalArtEntityNapalmBreath.class,new OrbalRenderNapalmBreath());
		RenderingRegistry.registerEntityRenderingHandler(OrbalArtEntityForte.class,new OrbalRenderForte());
	}
	@Override
	public String getCurrentLanguage() {
		return StringTranslate.getInstance().getCurrentLanguage();
	}
	@Override
	public void onTickInGame() {
		Minecraft minecraft = FMLClientHandler.instance().getClient();
		if (minecraft.currentScreen != null) {
			onTickInGUI(minecraft.currentScreen);
		}

		if (minecraft.thePlayer != null) {
			PlayerHandler.addPlayer(getPlayer().username, new PlayerHandler(getPlayer(), new PlayerStatistics()));
			
				if (loadedMod == false) {
					if (changedGui == false) {
						minecraft.ingameGUI = new GuiTheOrbalEra();
						changedGui = true;
					}

					sessionPlayer = getPlayer().username;
					loadedMod = true;
				}
				ranGame = true;
			
			
			if(OrbalUtils.isClientHost() == true) {
				String[] usernames = FMLCommonHandler.instance().getMinecraftServerInstance().getAllUsernames();
				for(int i = 0; i < usernames.length; i++) {
					if(PlayerHandler.getPlayer(usernames[i]) != null) {
						PlayerHandler.getPlayer(usernames[i]).getStats().onTick(getPlayer(), getPlayerMp(usernames[i]));
					}
				}
			} 
			else{ 
				PlayerHandler.getPlayer(sessionPlayer).getStats().onTick(getPlayer(), null);
			}
			
			inTick++;
			if (PacketPlayerDataSave.isRemote == true && inTick > 20) {
				if(FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT) {
					PacketPlayerDataSave.sendSaveData(getPlayer().username, PlayerHandler.getPlayer(getPlayer().username).getStats());
				inTick=0;
				}
			}
			

			
		}
	}

	
	public void onTickInGUI(GuiScreen gui) {
		if (gui instanceof GuiMainMenu) {
			PlayerHandler.ALL_PLAYERS.clear();
			loadedMod = false;
		}
	}
	
	
	/**
	 * Gets an EntityPlayer.class instance.
	 * @return EntityPlayer.class instance.
	 */
	public static EntityPlayer getPlayer() {
		return FMLClientHandler.instance().getClient().thePlayer;
	}

	/**
	 * Gets an EntityPlayerMP.class instance. Only works client server.
	 * @return EntityPlayerMP.class instance.
	 */
	public static EntityPlayerMP getPlayerMp(String username) {
		MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance();
		EntityPlayerMP player = server.getConfigurationManager().getPlayerForUsername(username);
		return player;
	}

	
}